/*
 * *********************************************************
 *   author   colin
 *   company  telchina
 *   email    wanglin2046@126.com
 *   date     18-1-9 下午5:16
 * ********************************************************
 */

package com.app.initial.amodule.init;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.app.initial.R;
import com.app.initial.amodule.base.ActivityParam;
import com.app.initial.amodule.base.BaseActivity;
import com.app.initial.amodule.main.activity.MainActivity;
import com.zcolin.frame.http.ZHttp;
import com.zcolin.frame.imageloader.ImageLoaderUtils;
import com.zcolin.frame.util.ActivityUtil;

import java.lang.ref.WeakReference;

/**
 * 初始加载页
 */
@ActivityParam(isFullScreen = true)
public class InitActivity extends BaseActivity {
    private long minReachTime = 2000;           //在此页的最少停留时间
    private boolean isExit;                     //是否已经退出此页面，是否继续加载的标志
    private String  requestTag;                 //http请求标志
    private boolean isLoadComplete;             //异步加载业务数据完成
    private boolean isReachMinimumTime;         //达到最低显示时间
    private boolean isAutoLoginComplete;        //如果不执行autoLogin操作，默认为完成

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);

        ImageView imageView = getView(R.id.imageView);

        //设置加载图片
        ImageLoaderUtils.displayImage(mActivity, "http://img5.duitang.com/uploads/item/201206/06/20120606175216_jLxBw.thumb.700_0.jpeg", imageView);

        //执行自动登录操作
        requestTag = autoLogin();

        //加载业务数据
        new InitAsyncTask(this).execute(this);

        //加载图要个中国式的最小加载时间
        new Handler().postDelayed(() -> {
            isReachMinimumTime = true;
            initComplete();
        }, minReachTime);
    }

    /**
     * 加载完成回调
     */
    private void initComplete() {
        if (isReachMinimumTime && isLoadComplete && isAutoLoginComplete && !isExit) {
            if (!onInitComplete()) {
                ActivityUtil.startActivity(mActivity, MainActivity.class);
                this.finish();
            }
        }
    }

    /**
     * 后台加载异步任务类
     */
    private static class InitAsyncTask extends AsyncTask<Context, Integer, Message> {
        WeakReference<InitActivity> weakReference;

        InitAsyncTask(InitActivity initActivity) {
            weakReference = new WeakReference<>(initActivity);
        }

        @Override
        protected Message doInBackground(Context... params) {
            if (weakReference != null && weakReference.get() != null) {
                weakReference.get().asyncLoadingInThread();
            }
            Message msg = new Message();
            msg.what = 0;
            return msg;
        }

        @Override
        protected void onPostExecute(Message result) {
            if (result.what == 0 && weakReference.get() != null && !weakReference.get().isExit) {
                weakReference.get().isLoadComplete = true;
                weakReference.get().initComplete();
            }
        }
    }

    @Override
    public void onBackPressed() {
        isExit = true;
        if (requestTag != null) {
            ZHttp.cancelRequest(requestTag);
        }

        mActivity.finish();
    }

    /**
     * 执行自动登录操作，登录认证结束时调用此方法
     */
    protected final void setAutoLoginComplete() {
        isAutoLoginComplete = true;
    }

    /**
     * 子线程中异步加载数据
     */
    protected void asyncLoadingInThread() {

    }

    /**
     * 加载完成回调
     *
     * @return 是否拦截默认加载完成操作，默认操作为关闭当前页面，启动MainActivity
     */
    protected boolean onInitComplete() {
        return false;
    }

    /**
     * 执行自动登录操作
     *
     * @return 登录请求返回的tag
     */
    protected String autoLogin() {
        setAutoLoginComplete();
        return null;
    }
}
